﻿using System.Collections.Generic;
using System.Diagnostics;

namespace DigitalQuran.Collection
{
    /// <summary>
    /// Represents ReadOnly Collection of Items
    /// </summary>
    /// <typeparam name="T">The type of elements in the collection</typeparam>
    [DebuggerDisplay("Count {Count}")]
    public abstract class ReadOnlyCollection<T> : IEnumerable<T>
    {
        public ReadOnlyCollection(int count)
        {
            Count = count;
        }

        /// <summary>
        /// Gets the Count
        /// </summary>
        public int Count { get; protected set; }

        /// <summary>
        /// Gets the Item at specified Index
        /// </summary>
        /// <param name="index">The zero-based index of the element to get</param>
        /// <returns>Returns Item at specified Index</returns>
        public abstract T this[int index] { get; }
        
        public abstract IEnumerator<T> GetEnumerator();

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public int IndexOf(T item) {
            for (int i = this.Count - 1; i >= 0; i--) {
                if (this[i].Equals(item))
                    return i;
            }

            return -1;
        }

        public bool Contains(T item) {
            for (int i = this.Count - 1; i >= 0; i--) {
                if (this[i].Equals(item))
                    return true;
            }

            return false;            
        }
    }
}